(function () {
    'use strict';
    angular.module('MainApp').run(initialize);

    initialize.$inject = ['$rootScope', 'settings', '$state', 'store', 'jwtHelper'];

    /**
     * @desc 模块初始化程序
     * @memberOf MainApp
     */
    function initialize($rootScope, settings, $state, store, jwtHelper) {
        $rootScope.$state = $state; // state to be accessed from view
        $rootScope.$settings = settings; // state to be accessed from view

        $rootScope.$on('$stateChangeStart',
            /**
             * @name 函数名
             * @desc 功能描述
             * @param {类型} 参数名 参数说明
             * @returns {类型}
             * @memberOf MainApp.initialize
             */
            function (e, toState) {
                if (toState.data && toState.data.requiresLogin) {
                    if (!store.get('JWT-TOKEN') || jwtHelper.isTokenExpired(store.get('JWT-TOKEN'))) {
                        e.preventDefault();
                        settings.layout.pageLogin = true;
                        $state.go('home.login', {from: toState.name});
                    }
                }
            });
    }
})();